<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
*/
//error_reporting(2047);
class Feedback extends MY_Controller{

	public function __construct() {
		parent::__construct();
		$this->load->model('Feedback_model','f');
		$this->load->library('form_validation');
		if ($this->isAdmin() == false) redirect("admin/login");
	}

	public function index()
	{
		if (!$this->admin_priv('feedback'))
		{
			show_error("您没有权限进行此操作！");
		}
		$sdata = array();
		$list = $this->f->L($sdata,'*',$this->_p['pagenumb'], ($this->_p['pagecur'] == 0) ? 0 : ($this->_p['pagecur']-1) * $this->_p['pagenumb']);
		$this->_p['pagecount'] = $this->input->post('pagecount','ctime','desc');
		if (empty($this->_p['pagecount'])) 
		{
			$this->_p['pagecount'] = $this->f->C($sdata);
		}
		$this->_d['page'] = eyPage($this->_p,$sdata);
		$this->_d['list'] = $list;
		$this->_d['pagecount'] = $this->_p['pagecount'];
		$this->load->view($this->_d['cfg']['tpl__admin'] . 'feedback/tlist', $this->_d);
	}

	public function reply($id)
	{
		if (!$this->admin_priv('feedback'))
		{
			show_error("您没有权限进行此操作！");
		}
		if ($this->form_validation->run('production/reply') == false)
		{
			if (validation_errors() != '')
			{
				$retmsg['code'] = '0';
				$retmsg['msg'] = validation_errors();
				exit(json_encode($retmsg));
			}
			$this->_d['row'] = $this->f->O(array('id'=>$id));
			$this->load->view($this->_d['cfg']['tpl__admin'] . 'feedback/detail', $this->_d);
		}
		else
		{
			$postdata = $this->input->post();
			
			if ($postdata['oldstatus'] != $postdata['is_reply'])
			{
				if ($postdata['is_reply'] == '1')
				{
					if (empty($this->_d['cfg']['smtp_server']))
					{
						$retmsg['code'] = '0';
						$retmsg['msg'] = '发邮件需要设置你的SMTP参数';
						exit(json_encode($retmsg));
					}
					if (empty($this->_d['cfg']['admin_email']))
					{
						$retmsg['code'] = '0';
						$retmsg['msg'] = '请设置你的管理员邮箱';
						exit(json_encode($retmsg));
					}
				}
			}
			if ($this->f->M($postdata,array('id'=>$id)))
			{
				if ($postdata['oldstatus'] != $postdata['is_reply'])
				{
					if ($postdata['is_reply'] == '1')
					{
						// 发送邮箱
						$this->load->library('email'); //加载CI的email类 
						$emailinfo=array('protocol' => 'smtp',
							'smtp_host' => $this->_d['cfg']['smtp_server'],
							'smtp_user' => $this->_d['cfg']['smtp_username'],
							'smtp_pass' => $this->_d['cfg']['smtp_passwd'],
							'smtp_port' => '25',
							'charset' => 'utf-8',
							'wordwrap' => TRUE,
							'mailtype' => 'html');

						$this->email->initialize($emailinfo);
						//以下设置Email内容  
						$this->email->from($emailinfo['smtp_user']);  
						$this->email->to($postdata['email']);  
						$this->email->subject($postdata['reply_title']);
						$content = $postdata['reply_content'];
						$this->email->message($content);  
						$this->email->send();
						$retmsg['code'] = '1';
						$retmsg['msg'] = '已发回复邮件给客户';
						exit(json_encode($retmsg));
					}
					else
					{
						$retmsg['code'] = '1';
						$retmsg['msg'] = '修改成功';
						exit(json_encode($retmsg));
					}
				}
				else
				{

					$retmsg['code'] = '1';
					$retmsg['msg'] = '修改成功';
					exit(json_encode($retmsg));
				}
			}
		}
		
	}

	public function del($id)
	{
		if (!$this->admin_priv('feedback'))
		{
			show_error("您没有权限进行此操作！");
		}
		$sdata = array('id'=>$id);
		//$adinfo = $this->user->O($sdata);
		$this->f->D($sdata);
		$admininfo = $this->isAdmin();
		$this->action_log('1',$admininfo['user_id'],"删除留言");
		$retmsg['code'] = '1';
		$retmsg['msg'] = $this->lang->line('success');
		exit(json_encode($retmsg));
	}

	public function delMore()
	{
		if (!$this->admin_priv('feedback'))
		{
			show_error("您没有权限进行此操作！");
		}
		$postdata = $this->input->post('id');
		if(!empty($postdata))
		{
			if(is_array($postdata))
			{
			$id = implode(',',$postdata);
			}
			$this->f->D(array('id in(' . $id . ')'=>''));
			$admininfo = $this->isAdmin();
			$this->action_log('1',$admininfo['user_id'],"批量删除询价");
			$retmsg['code'] = '1';
			$retmsg['msg'] = $this->lang->line('success');
			exit(json_encode($retmsg));
		}
		else
		{
			$retmsg['code'] = '0';
			$retmsg['msg'] = $this->lang->line('fail');
			exit(json_encode($retmsg));
		}
	}

}

?>